package com.qdlc.p2p.dal.dao.impl;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Query;

import org.springframework.stereotype.Repository;

import com.qdlc.p2p.common.util.StringUtil;
import com.qdlc.p2p.dal.common.dao.jpa.BaseDaoImpl;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.common.model.jpa.QueryParam;
import com.qdlc.p2p.dal.common.model.jpa.SearchFilter.Operators;
import com.qdlc.p2p.dal.dao.RedEnvelopeDao;
import com.qdlc.p2p.dal.dto.RedEnvelope;
import com.qdlc.p2p.dal.model.RedEnvelopeModel;

/**
 * 红包方案DAO接口
 * @author ylx
 * @version 2.0
 * @since 2015年04月22日 13时52分37秒
 */
@Repository(value = "redEnvelopeDao")
public class RedEnvelopeDaoImpl extends BaseDaoImpl<RedEnvelope> implements RedEnvelopeDao {

    @SuppressWarnings("unchecked")
    @Override
    public RedEnvelope getRedEnvelopeById(long id) {
        String jpql = "from RedEnvelope where id = ?1";
        Query query = em.createQuery(jpql);
        query.setParameter(1, id);
        List<RedEnvelope> list = query.getResultList();
        if (list != null && list.size() >= 0) {
            return (RedEnvelope) list.get(0);
        } else {
            return null;
        }
    }

	@Override
	public List<RedEnvelope> findUsableRedEnvelope() {
		QueryParam param = QueryParam.getInstance();
		param.addParam("status", RedEnvelopeModel.RED_ENVELOPE_STATUS_ENABLE);
		param.addParam("isDelete", Operators.NOTEQ, RedEnvelopeModel.RED_ENVELOPE_IS_DELETE);
		return findByCriteria(param);
	}
	
	@Override
	public List<RedEnvelope> findUsableRedEnvelopeForFixed() {
		QueryParam param = QueryParam.getInstance();
		param.addParam("status", RedEnvelopeModel.RED_ENVELOPE_STATUS_ENABLE);
		param.addParam("payType", RedEnvelopeModel.RED_ENVELOPE_PAY_FIXED_AMOUNT);
		param.addParam("isDelete", Operators.NOTEQ, RedEnvelopeModel.RED_ENVELOPE_IS_DELETE);
		return findByCriteria(param);
	}

	@Override
	public List<RedEnvelope> findUsableRedEnvelopeForNoFixed() {
		QueryParam param = QueryParam.getInstance();
		param.addParam("status", RedEnvelopeModel.RED_ENVELOPE_STATUS_ENABLE);
		param.addParam("payType", Operators.NOTEQ, RedEnvelopeModel.RED_ENVELOPE_PAY_FIXED_AMOUNT);
		param.addParam("isDelete", Operators.NOTEQ, RedEnvelopeModel.RED_ENVELOPE_IS_DELETE);
		return findByCriteria(param);
	}
	
	@SuppressWarnings("static-access")
	@Override
	public PageDataList<RedEnvelopeModel> list(RedEnvelopeModel model) {
		QueryParam param = QueryParam.getInstance().addPage(model.getPage(),
				model.getRows());
		if(StringUtil.isNotBlank(model.getSearchName())){
			param.addParam("name", Operators.LIKE, model.getSearchName());
		}
		param.addParam("isDelete", Operators.NOTEQ, model.RED_ENVELOPE_IS_DELETE);
		PageDataList<RedEnvelope> dataList = this.findPageList(param);
		PageDataList<RedEnvelopeModel> dataList_ = new PageDataList<RedEnvelopeModel>();
		List<RedEnvelopeModel> list = new ArrayList<RedEnvelopeModel>();
		if (dataList.getList() != null && dataList.getList().size() > 0) {
			for (RedEnvelope redEnvelope : dataList.getList()) {
				RedEnvelopeModel envelopeModel = RedEnvelopeModel.instance(redEnvelope);
				list.add(envelopeModel);
			}
		}
		dataList_.setPage(dataList.getPage());
		dataList_.setList(list);
		return dataList_;
	}

	@Override
	public void delete(long id) {
		String jpql = "update RedEnvelope set isDelete = ?1 where id = ?2";
		Query query = em.createQuery(jpql);
		query.setParameter(1, RedEnvelopeModel.RED_ENVELOPE_IS_DELETE);
		query.setParameter(2, id);
		query.executeUpdate();
	}
	
	@Override
	public int update(byte status, byte prestatus,long id) {
		String jpql = "UPDATE RedEnvelope set status=:status WHERE status=:prestatus AND id=:id";
		int count = updateByJpql(jpql, new String[]{"status","prestatus","id"}, new Object[] {status,prestatus,id});
		return count;
	}
}
